package Group.projectA;

import java.io.File;
import java.util.Iterator;
import java.util.Scanner;

public class MainClass {
	private static AddressBook myAddressBook=new AddressBook();
	private static File f=new File("src/main/java/Group/projectA/d.txt");
	/**
	 * 打开的通讯录文件
	 */
	
	public static void showMainMenu() {
		show("**********通讯录************");
		show("主菜单");
		show("  1查看通讯录");
		show("  2增加联系人");
		show("  3删除联系人");
		show("  4修改联系人");
		show("  5查找联系人");
		show("  6保存通讯录");
		show("  7退出通讯录");
		System.out.println("请输入序号选择菜单：");
		Scanner keyin=new Scanner(System.in);
		int choose=keyin.nextInt();
		switch(choose){
		case 1:showAddressBook();
			   break;
		case 2:addNewPerson();
		       break;
		case 3:deletePerson();
		       break;
		case 4:upDatePerson();
			break;
		case 5:findPerson();
		       break;
		case 6:saveAddressBook();
		       break;
		case 7:System.exit(0);
		       break;
		default:showMainMenu();
		
		}
	}
		
	public static void show(String s) {
		System.out.println(s);
	}
	public static void enter(){
		new Scanner(System.in).nextLine();
	}
    
	public static void showAddressBook(){
		Iterator<Person> itor=myAddressBook.iterator();
		Person p;
		if(myAddressBook.size()==0){
			show("通讯录为空！按回车键返回主菜单");
			enter();
			showMainMenu();
			return;
		}
		int i=1;
		while(itor.hasNext()){
			p=itor.next();
			showPerson(p);
			if(0==i++%10)
				enter();
			/**
			 * 每次显示10个人
			 */
		}
		show("\n\b按回车键返回主菜单!");
		enter();
		showMainMenu();
	}
	public static void showPerson(Person p){
		show("姓名"+p.getName()+"性别"+p.getSex()+"地址"+p.getAddress()+"电话号码"+p.getPhoneNum()+"Email"+p.getEmail());;
	}
	public static void addNewPerson() {
		show("--------------增加联系人---------------");
		show("输入联系人信息：");
		System.out.println("姓名：");
		String name=new Scanner(System.in).next();
		System.out.println("性别：");
		String sex=new Scanner(System.in).next();
		System.out.println("地址：");
		String address=new Scanner(System.in).next();
		System.out.println("电话号码：");
		String phoneNum=new Scanner(System.in).next();
		System.out.println("email：");
		String email=new Scanner(System.in).next();
		Person p=new Person(name,sex,address,phoneNum,email);
		showPerson(p);
		System.out.println("是否确认创建？请输入“是(y)/否(n)”");
		String y=new Scanner(System.in).next();
		if("y".equals(y)){
			myAddressBook.addPerson(p);
			show("联系人添加成功！按回车键返回主菜单");
			enter();
			showMainMenu();
		}else{
			show("不添加联系人！按回车键返回主菜单");
			enter();
			showMainMenu();
		}
	}
	
    private static void deletePerson() {
		show("-------------删除联系人-------------");
		show("请输入需要删除的联系人姓名：");
		String name=new Scanner(System.in).next();
		Person person[]=myAddressBook.findPerson(name);
		if(person.length==0){
			show("没有符合要求的联系人，按回车键返回主菜单");
			enter();
			showMainMenu();
			return;
		}
		for(Person p:person){
		showPerson(p);
		System.out.println("确认删除？是（y）/否（n）");
		String y=new Scanner(System.in).next();
		if("y".equals(y)){
			myAddressBook.deletePerson(p);
			show("联系人删除成功！按回车键返回主菜单");
			enter();
			showMainMenu();
		}else{
			show("不删除联系人！按回车键返回主菜单");
			enter();
			showMainMenu();
		}
		}
	}
    
    private static void upDatePerson() {
    	show("--------------修改联系人-------------");
		show("请输入需要修改的联系人姓名：");
		String name1=new Scanner(System.in).next();
		Person person[]=myAddressBook.findPerson(name1);
		if(person.length==0){
			show("没有符合要求的联系人，按回车键返回主菜单");
			enter();
			showMainMenu();
			return;
		}
				
		for(Person p:person){
		showPerson(p);
				
		System.out.println("姓名：");
		String name=new Scanner(System.in).next();
		System.out.println("性别：");
		String sex=new Scanner(System.in).next();
		System.out.println("地址：");
		String address=new Scanner(System.in).next();
		System.out.println("电话号码：");
		String phoneNum=new Scanner(System.in).next();
		System.out.println("email：");
		String email=new Scanner(System.in).next();
		Person p1=new Person(name,sex,address,phoneNum,email);
		
		System.out.println("确认修改？是（y）/否（n）");
		String y=new Scanner(System.in).next();
		if("y".equals(y)){
			myAddressBook.updatePerson(p,p1);
			myAddressBook.deletePerson(p);
			show("联系人修改成功！按回车键返回主菜单");
			enter();
			showMainMenu();
		}else{
			show("不修改联系人！按回车键返回主菜单");
			enter();
			showMainMenu();
		}
		}
		
		
	}

	public static void findPerson() {
		show("---------------------查找联系人-------------------");
		System.out.println("请输入联系人姓名");
		String search=new Scanner(System.in).next();
		Person person[]=myAddressBook.findPerson(search);//查找联系人
		if(person.length==0){
			show("没有符合要求的联系人，按回车键返回主菜单");
			enter();
			showMainMenu();
			return;
		}
		int i=0;
		for(Person p:person){
			showPerson(p);
			if(0==i++%10)
				enter();
			/**
			 * 每次显示10个人
			 */
		}
		enter();
		showMainMenu();
	}
	
	
	public static void saveAddressBook() {

		myAddressBook.saveAddressBook(f);
		show("保存成功！按回车键返回主菜单");
		enter();
		showMainMenu();
	}
	public static void main(String[] args) {
		myAddressBook.loadAddressBook(f);
		showMainMenu();
	}

}
